﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Utils
{
    public class ImageHelper
    {
        /// <summary>
        /// 生成缩略图(缩小原图到指定范围)
        /// </summary>
        /// <param name="srcFile">原图路径</param>
        /// <param name="destFile">缩略图路径</param>
        /// <param name="maxWidth">缩略图最大宽度</param>
        /// <param name="maxHeight">缩略图最大高度</param>
        /// <returns></returns>
        public static bool GetFixedRatioImage(string srcFile, string destFile, int maxWidth, int maxHeight)
        {
            if (!File.Exists(srcFile))
                return false;

            int destWidth = 0, destHeight = 0;

            using (Image image = Image.FromFile(srcFile))
            {
                if (image.Width > maxWidth && image.Height > maxHeight)
                {
                    destWidth = maxWidth;
                    destHeight = maxWidth * image.Height / image.Width;

                    if (destHeight > maxHeight)
                    {
                        destWidth = maxHeight * image.Width / image.Height;
                        destHeight = maxHeight;
                    }
                }
                else
                {
                    destWidth = image.Width;
                    destHeight = image.Height;
                }

                using (Image bitmap = new Bitmap(destWidth, destHeight))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        graphics.InterpolationMode = InterpolationMode.High;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        graphics.Clear(Color.Transparent);
                        graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                        bitmap.Save(destFile, ImageFormat.Jpeg);
                    }
                }
            }

            return true;
        }

        /// <summary>
        /// 生成缩略图(裁切原图并缩小到指定尺寸)
        /// </summary>
        /// <param name="srcFile">原图路径</param>
        /// <param name="destFile">缩略图路径</param>
        /// <param name="destWidth">缩略图宽度</param>
        /// <param name="destHeight">缩略图高度</param>
        /// <returns></returns>
        public static bool GetFixedSizeImage(string srcFile, string destFile, int destWidth, int destHeight)
        {
            if (!File.Exists(srcFile))
                return false;

            int srcWidth = 0, srcHeight = 0;

            using (Image image = Image.FromFile(srcFile))
            {
                if (image.Width > destWidth && image.Height > destHeight)
                {
                    if ((float)image.Width / (float)destWidth > (float)image.Height / (float)destHeight)
                    {
                        srcWidth = image.Height * destWidth / destHeight;
                        srcHeight = image.Height;
                    }
                    else
                    {
                        srcWidth = image.Width;
                        srcHeight = image.Width * destHeight / destWidth;
                    }
                }
                else
                {
                    srcWidth = image.Width;
                    srcHeight = image.Height;
                    destWidth = image.Width;
                    destHeight = image.Height;
                }

                using (Image bitmap = new Bitmap(destWidth, destHeight))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        graphics.InterpolationMode = InterpolationMode.High;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        graphics.Clear(Color.Transparent);
                        graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
                        bitmap.Save(destFile, ImageFormat.Jpeg);
                    }
                }
            }

            return true;
        }
    }
}